#include "DataState.h"

#include "StringUtility.h"

DataState::DataState()
{
    name = "";
    description = "";
}

DataState::DataState(CRstring nameStr, CRstring descriptionStr, CRstring bgImage, CRstring iconImage)
{
    name = nameStr;
    description = descriptionStr;
    loadBackground(bgImage);
    loadIcon(iconImage);
}

DataState::~DataState()
{
    //
}


void DataState::loadBackground(CRstring bgImage)
{
    loadImageFromToken(bgImage,bg);
}

void DataState::loadIcon(CRstring iconImage)
{
    loadImageFromToken(iconImage,icon);
}

void DataState::loadImageFromToken(CRstring token, AnimatedSprite& target)
{
    if (token == "")
        return;

    bool error = false;
    vector<string> split;
    StringUtility::tokenize(token,split,",");

    if (split.size() >= 3) // animation
    {
        target.loadFrames(split[0],StringUtility::stringToInt(split[1]),StringUtility::stringToInt(split[2]));

        if (split.size() >= 4) // transparent colour specified
            target.setTransparentColour(Colour(StringUtility::stringToInt(split[3])));
        if (split.size() >= 5) // frame-rate specified
            target.setTimerScaler(1000.0f / StringUtility::stringToFloat(split[4]));
        if (split.size() >= 6)
            error = true;
    }
    else // single image
    {
        if (split.size() > 1)
            error = true;
        target.loadFrame(split[0]);
    }

    if (error)
    {
        cout << "Error loading image for state: A wrong number of arguments have been given: " << StringUtility::intToString(split.size()) << " (should be 1, 3, 4 or 5)" << endl;
        cout << "The token was: " << token << endl;
    }
}
